package top.mask.mq.three;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import top.mask.mq.utils.RabbitMQUtil;
import top.mask.mq.utils.SleepUtil;

/**
 * 消息在手动应答时是不丢失，放回队列中重新消费
 */
@Slf4j
public class Work04 {
    //队列名称
    public static final String TASK_QUEUE_NAME = "ack_queue";


    @SneakyThrows
    public static void main(String[] args) {
        Channel channel = RabbitMQUtil.getChannel();
        log.info("C2等待接收的时间处理时间较长");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            //睡眠一秒
            SleepUtil.sleep(30);
            log.info("接收消息[{}]成功",new String(message.getBody(),"UTF-8"));
            /**
             * 手动应答
             * 1.消息的标记 tag
             * 2.是否批量应答 false，不批量应答信道中的信息 true批量
             */
//            channel.basicQos(1);
            channel.basicQos(5);
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);

        };


        CancelCallback cancelCallback = consumerTag ->
                log.info("消息者取消消接口回调逻辑{[]}",consumerTag);

        //采用手动应答
        boolean autoAck = false;
        channel.basicConsume(TASK_QUEUE_NAME,autoAck,deliverCallback,cancelCallback);
    }
}
